package cn.iocoder.yudao.module.member.agency.service;

import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreLambdaQueryWrapper;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreQueryWrapper;
import cn.iocoder.yudao.framework.business.basic.pojo.vo.UserSimpleVO;
import cn.iocoder.yudao.framework.business.eligibility.service.AdminEligibilityBaseService;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.qrcode.pojo.QrCodeAgencyType;
import cn.iocoder.yudao.framework.qrcode.util.QrCodeUtils;
import cn.iocoder.yudao.module.api.member.agency.vo.AgencyUserVO;
import cn.iocoder.yudao.module.api.member.user.MemberUserApi;
import cn.iocoder.yudao.module.member.agency.dal.dataobject.AgencyUserDO;
import cn.iocoder.yudao.module.member.agency.dal.mysql.AgencyUserMapper;
import cn.iocoder.yudao.module.member.agency.vo.AdminAgencyUserQueryDTO;
import cn.iocoder.yudao.module.member.agency.vo.AdminAgencyUserVO;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

/**
 * 招商人员信息 Service 实现类
 *
 * @author 山野羡民
 */
@Service
@Validated
public class AdminAgencyUserServiceImpl extends AdminEligibilityBaseService<AgencyUserDO, AdminAgencyUserVO, AdminAgencyUserQueryDTO> implements AdminAgencyUserService {
    @Resource
    private AgencyUserMapper agencyUserMapper;

    @Resource
    private MemberUserApi memberUserApi;

    @Override
    public AgencyUserMapper getInjectedMapper() {
        return agencyUserMapper;
    }

    @Nonnull
    @Override
    protected CoreQueryWrapper<AgencyUserDO> buildQueryWrapper(AdminAgencyUserQueryDTO param) {
        CoreQueryWrapper<AgencyUserDO> wrapper = super.buildQueryWrapper(param);
        wrapper.gt("purchase_count", 0);
        wrapper.eqIfPresent("level", param.getLevel());
        if (param.getValid() != null) {
            if (param.getValid()) {
                wrapper.gt("expire_time", LocalDateTime.now());
            } else {
                wrapper.le("expire_time", LocalDateTime.now());
            }
        }
        return wrapper;
    }

    @Override
    public List<AdminAgencyUserVO> convertToViewObject(@Nonnull List<AgencyUserDO> entities) {
        List<AdminAgencyUserVO> vos = super.convertToViewObject(entities);
        Set<Long> userIds = convertSet(vos, AdminAgencyUserVO::getUserId);
        Map<Long, UserSimpleVO> users = getUserSimpleMap(userIds);
        Map<Long, AgencyUserVO> agencyUsers = CollStreamUtil.toMap(getListByUserIds(userIds),
                AgencyUserVO::getUserId, dto -> dto);
        for (AdminAgencyUserVO vo : vos) {
            UserSimpleVO userSimpleVO = users.getOrDefault(vo.getUserId(), UserSimpleVO.ANONYMOUS);
            AgencyUserVO agencyUserVO = agencyUsers.getOrDefault(vo.getUserId(), new AgencyUserVO());
            QrCodeAgencyType qrCodeAgencyType = new QrCodeAgencyType();
            qrCodeAgencyType.setAgencyId(agencyUserVO.getId());
            qrCodeAgencyType.setMemberNo(userSimpleVO.getNo());
            vo.setQrcode(QrCodeUtils.generatePngAsBase64Uri(qrCodeAgencyType, userSimpleVO.getAvatar()));
        }
        return vos;
    }

    @Nonnull
    @Override
    protected Map<Long, UserSimpleVO> getUserSimpleMap(Collection<Long> userIds) {
        return memberUserApi.getUserSimpleMap(userIds);
    }

    @Override
    public List<AgencyUserVO> getListByUserIds(Set<Long> agencyUserIds) {
        if (CollUtil.isEmpty(agencyUserIds)) {
            return null;
        }
        CoreLambdaQueryWrapper<AgencyUserDO> wrapper = new CoreLambdaQueryWrapper<>();
        wrapper.inIfPresent(AgencyUserDO::getUserId, agencyUserIds);
        List<AgencyUserDO> list = agencyUserMapper.selectList(wrapper);
        return BeanUtils.toBean(list, AgencyUserVO.class);
    }

}